package com.zhiyou100.javaweb.myservlet.homework.day004.studentmanagementsystem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @packageName: javase_26
 * @className: ${PACKAGE_NAME}
 * @Description: TODO 修改一个
 * @author: yang
 * @date: 2020/5/26
 */
@WebServlet(name = "Day004ServletAlterOne", urlPatterns = "/day04/alterOne")
public class Day004ServletAlterOne extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 1 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 2 设定类型
        StudentDao studentDao = new StudentDao();
        // 3 创建dao对象
        String studentId = request.getParameter("studentId");
        String studentName = request.getParameter("studentName");
        String studentGender = request.getParameter("studentGender");
        String studentScore = request.getParameter("studentScore");
        // 获取参数
        boolean b = studentDao.queryOne(Integer.parseInt(studentId));
        // 判断是否存在
        PrintWriter writer = response.getWriter();
        String massage = "";
        if (!b) {
            // 不如果存在
            massage = "<h1>该id不存在，修改失败，请先添加</h1>";
        } else {
            Student student = new Student(Integer.parseInt(studentId), studentName, studentGender, Double.parseDouble(studentScore));
            boolean b1 = studentDao.alterOne(student);
            if (b1) {
                massage = "<h1>修改成功</h1>";
            }
        }
        writer.write(massage);
        writer.flush();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
